package com.shiyi.utils;

import org.lionsoul.ip2region.xdb.Searcher;
import java.io.IOException;

public class Ip2RegionExample {



    public static void main(String[] args) {
        // 1. 定义 IP 数据库文件路径
        String dbPath = "E:\\我的世界\\我的博客\\my-blog-service\\common\\src\\main\\resources\\ip2region.xdb"; // 替换为你的文件路径

        // 2. 创建 Searcher 对象
        Searcher searcher = null;
        try {
            searcher = Searcher.newWithFileOnly(dbPath);
        } catch (IOException e) {
            System.out.println("Failed to create searcher: " + e.getMessage());
            return;
        }

        // 3. 定义要解析的 IP 地址
        String ip = "61.241.69.213"; // 替换为需要解析的 IP

        // 4. 解析 IP 地址
        try {
            String region = searcher.search(ip);
            System.out.println("IP: " + ip + ", 地理位置: " + region);
        } catch (Exception e) {
            System.out.println("Failed to search IP: " + e.getMessage());
        } finally {
            // 5. 关闭 Searcher
            try {
                if (searcher != null) {
                    searcher.close();
                }
            } catch (IOException e) {
                System.out.println("Failed to close searcher: " + e.getMessage());
            }
        }
    }
}